/**
 * 着色器加载器 - 简化版本，使用内置着色器
 */
export class ShaderLoader {
  constructor() {
    this.shaders = {
      vertex: {},
      fragment: {},
      simulation: {}
    }
    this.onShadersLoaded = null
  }

  /**
   * 加载着色器
   */
  loadShaders() {
    // 使用内置着色器代替文件加载
    this.shaders.vertex.node = this.getNodeVertexShader()
    this.shaders.fragment.node = this.getNodeFragmentShader()
    this.shaders.vertex.edge = this.getEdgeVertexShader()
    this.shaders.fragment.edge = this.getEdgeFragmentShader()
    
    // 模拟异步加载
    setTimeout(() => {
      if (this.onShadersLoaded) {
        this.onShadersLoaded()
      }
    }, 100)
  }

  /**
   * 节点顶点着色器
   */
  getNodeVertexShader() {
    return `
      attribute vec3 position;
      attribute vec3 color;
      attribute float size;
      attribute float nodeId;
      
      uniform mat4 modelViewMatrix;
      uniform mat4 projectionMatrix;
      uniform float currentTime;
      uniform sampler2D positionTexture;
      uniform sampler2D nodeAttribTexture;
      
      varying vec3 vColor;
      varying float vNodeId;
      
      void main() {
        vColor = color;
        vNodeId = nodeId;
        
        vec3 pos = position;
        
        // 如果有位置纹理，使用纹理中的位置
        // 这里简化实现，直接使用attribute中的位置
        
        vec4 mvPosition = modelViewMatrix * vec4(pos, 1.0);
        gl_Position = projectionMatrix * mvPosition;
        
        // 根据距离调整点的大小
        gl_PointSize = size * (300.0 / -mvPosition.z);
        gl_PointSize = max(gl_PointSize, 2.0);
        gl_PointSize = min(gl_PointSize, 50.0);
      }
    `
  }

  /**
   * 节点片段着色器
   */
  getNodeFragmentShader() {
    return `
      precision mediump float;
      
      varying vec3 vColor;
      varying float vNodeId;
      
      uniform float currentTime;
      
      void main() {
        // 创建圆形点
        float dist = distance(gl_PointCoord, vec2(0.5));
        if (dist > 0.5) discard;
        
        // 边缘渐变效果
        float alpha = 1.0 - smoothstep(0.3, 0.5, dist);
        
        // 添加脉动效果
        float pulse = 0.8 + 0.2 * sin(currentTime * 0.005 + vNodeId * 0.1);
        
        gl_FragColor = vec4(vColor * pulse, alpha);
      }
    `
  }

  /**
   * 边顶点着色器
   */
  getEdgeVertexShader() {
    return `
      attribute vec3 position;
      attribute vec3 color;
      attribute float weight;
      
      uniform mat4 modelViewMatrix;
      uniform mat4 projectionMatrix;
      uniform sampler2D positionTexture;
      uniform sampler2D nodeAttribTexture;
      
      varying vec3 vColor;
      varying float vWeight;
      
      void main() {
        vColor = color;
        vWeight = weight;
        
        vec3 pos = position;
        
        vec4 mvPosition = modelViewMatrix * vec4(pos, 1.0);
        gl_Position = projectionMatrix * mvPosition;
      }
    `
  }

  /**
   * 边片段着色器
   */
  getEdgeFragmentShader() {
    return `
      precision mediump float;
      
      varying vec3 vColor;
      varying float vWeight;
      
      uniform float currentTime;
      
      void main() {
        // 根据权重调整透明度
        float alpha = 0.3 + 0.4 * min(1.0, vWeight / 10.0);
        
        gl_FragColor = vec4(vColor, alpha);
      }
    `
  }

  /**
   * 获取着色器
   */
  getShader(type, name) {
    return this.shaders[type] && this.shaders[type][name]
  }

  /**
   * 检查着色器是否已加载
   */
  isLoaded() {
    return this.shaders.vertex.node && 
           this.shaders.fragment.node && 
           this.shaders.vertex.edge && 
           this.shaders.fragment.edge
  }
}
